Skip to main content

Operators

Operator .?

Queries that include arrays of Boolean values in combination with other select queries may not always output the result you have expected. Let's take a closer look at the following examples:

Let's consider the following polymorphic array ENTRIES with objects LIMIT and MARKET.

entries: [
Limit{price:10, size:1},
Market{size:2},
Limit{price:20, size:3}
]

In this example, let's create a QQL query that returns only objects with Price > 15, which is Limit{price:20, size:3}.

This query returns the desired output Limit{price:20, size:3}.

SELECT entries[price > 15]

This query returns the wrong output [Market{size:2}].

SELECT entries[entries.price > 15]

Let's take this query apart to explain:

This part of the query as it is will return just price values 10 and 20.

SELECT entries.price

If we put it like this, it will output an array of true/false values [false, true], based just on the previous query output, which was 10 and 20 (20 is greater than 15, this is why we are getting true for the second value).

SELECT entries.price > 15

If we put it like this, we are getting the unexpected result of [Market{size:2}], because it picks the second element in the entries array based on [false, true] output of the previous query.

SELECT entries[entries.price > 15] == SELECT entries[[false, true]]

We can rewrite the above queries using .? operator to output the desired value Limit{price:20, size:3}.

SELECT entries[entries.?price > 15]

Let's take this query apart to explain:

This query alone will return an array [10, null, 20]. We get NULL because the second entries array element [Market{size:2}] does not meet the condition.

SELECT entries.?price

If we rewrite it the following way it will return the array of true/false values: [false, false, true], based on the previous query output (20 is greater than 15, this is why we are getting true for the third value).

SELECT entries.?price > 15

This is why this query will return the third element in the entries array, which is Limit{price:20, size:3}, based on [false, false, true] from the previous query output.

SELECT entries[entries.?price > 15] == SELECT entries[[false, false, true]]

In this example we used .? operator. Queries with this operator return objects not meeting the specified criteria as NULL values. In our example it was Market{size:2}. This enables us getting a correct dataset in combination with select queries that return an array of Boolean values such as entries.?price > 15 in our example.

Basic

Below, there is a list of basic operations supported by QQL for simple objects and arrays. For arrays we use per-element calculations.

ExpressionExplanation
- xnegation
x + yaddition
x - ysubtraction
x * ymultiplication
x / ydivision
x % ymodulus
  • x % y - works only for integer types.
  • x / y - works as integer division in case of integer types. In case at least one of the operands is a float (decimal), works as a regular division.
#returns a new field Mult as a result of stream element multiplication
SELECT (entries.price * entries.size) AS Mult FROM packages

Comparison

Comparison operators. For arrays and constants we use per-element calculations.

ExpressionExplanationImplementationResult
x == yequalsimple types and arraysboolean, boolean array
x != ynot equalsimple types and arraysboolean, boolean array
x === ystrict equalsimple types and arraysboolean
x !== ystrict not equalsimple types and arraysboolean
x > ygreatersimple types and arraysboolean, boolean array
x < ylesssimple types and arraysboolean, boolean array
x >= ygreater or equalsimple types and arraysboolean, boolean array
x <= yless or equalsimple types and arraysboolean, boolean array
list1:[1,2,3] === list2[1,2,3] -> true
list1:[1,2,3] == list2:[1,2,3] -> [true, true, true]
list1:[1,2,3] === int1:3 -> false
list1:[1,2,3] == int1:3 -> [false, false, true]

#Check all Entries array Price elements against the condition. Return boolean array.
SELECT entries.price > 2000 FROM packages

IN and LIKE Operators

Use IN operator to check whether the query output matches the specified set of values.

Use LIKE operator to check whether String matches the defined template. Template may include special characters like % (any number of any symbols) and _ (just specific symbols)

#select all stream messages where symbol is either LTCUSD or BTCUSD
SELECT * FROM binance
WHERE symbol IN ('LTCUSD', 'BTCUSD')

SELECT * FROM binance
ARRAY JOIN entries AS entry
WHERE entry.exchangeId IN ('KRAKEN', 'GDAX')

#select all stream messages where symbol name starts with BTC..., for example BTCUSD
SELECT * FROM binance
WHERE symbol LIKE 'BTC%'

#will not return BTCUSDT
SELECT * FROM binance
WHERE symbol LIKE 'BTC___'

Logical

Logical operators. For arrays and constants we use per-element calculations. Consumes only booleans or arrays of booleans, produces only booleans or arrays of booleans.

ExpressionExplanationResult
x and yconjunctionboolean, boolean array
x or ydisjunctionboolean, boolean array
not xnegationboolean, boolean array